/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view.dialogs;

import java.util.List;  
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.mags.common.EngineContext;
import org.mags.remas.model.entity.CommentType;

/**
 * 
 * @author Mario Gaitan
 *
 */
public abstract class AbstractFindCommentsDialog extends AbstractDialog {
    private Button cancelButton;
    private Button acceptButton;
    private Text commentText;
    private List<CommentType> commentTypeList;
    private Combo commentTypeCombo;
    
    public String getComment() {
    	return commentText.getText();
    }
    public CommentType getCommentType() {
    	return commentTypeCombo.getSelectionIndex()==0?null:commentTypeList.get(commentTypeCombo.getSelectionIndex()-1);
    }
    public AbstractFindCommentsDialog(Display display) {
    	super(display, SWT.RESIZE);
    }
    protected void createPartControl(Composite parent)  {
    	parent.setLayout(new GridLayout(2, true));
    	Label label = new Label(parent, SWT.NONE);
    	label.setText("comment");
    	label.setLayoutData(getCellLayoutData());
    	commentText = new Text(parent, SWT.BORDER);
    	commentText.setTextLimit(255);
    	commentText.setLayoutData(getCellLayoutData());

    	label = new Label(parent, SWT.NONE);
    	label.setText("comment type");
    	label.setLayoutData(getCellLayoutData());
		commentTypeList = EngineContext.getDataContext().getEntityProviderFactory().getCommentTypeProvider().getAll();
		commentTypeCombo = new Combo(parent, SWT.BORDER|SWT.DROP_DOWN|SWT.READ_ONLY);
		commentTypeCombo.setLayoutData(getCellLayoutData());
		commentTypeCombo.add("-no selected-");
		for(CommentType i: commentTypeList)
			commentTypeCombo.add(i.getDescription());
		commentTypeCombo.select(0);
    	createButtons(parent);
    }
    private GridData getCellLayoutData() {
		GridData cellLayoutData = new GridData();
		cellLayoutData.horizontalAlignment = SWT.FILL;
		cellLayoutData.grabExcessHorizontalSpace = true;
		return cellLayoutData;
    }    
    private void createButtons(Composite parent) {
		cancelButton = new Button(parent, SWT.NONE);
		cancelButton.setText("show all");
		cancelButton.setLayoutData(getCellLayoutData());
		acceptButton = new Button(parent, SWT.NONE);
		acceptButton.setText("accept");
		acceptButton.setLayoutData(getCellLayoutData());
		cancelButton.addSelectionListener(new SelectionListener(){
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			@Override
			public void widgetSelected(SelectionEvent e) {
				cancel();
			}
		});
		acceptButton.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			@Override
			public void widgetSelected(SelectionEvent e) {
				accept();
			}
		});    	
    }    
    public abstract void accept();
    public abstract void cancel();
}
